//
//  IntroduceViewController.m
//  JuYouQian
//
//  Created by hisuntech on 14-12-18.
//  Copyright (c) 2014年 hisuntech. All rights reserved.
//


#import "IntroduceViewController.h"
#import "IntroducePageView.h"
#import "View+MASAdditions.h"
#import "WebViewController.h"

#import "HeConstants.h"
#import "SessionMgr.h"
#import "HePreferrence.h"


@interface IntroduceViewController ()

@end

@implementation IntroduceViewController

@synthesize pageControl;
@synthesize imageArray;
@synthesize isAdd;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.isAdd = YES;
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];

}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if (!isAdd){
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

-(void)viewDidLayoutSubviews
{
    NSArray *arIntroducePage = [[NSBundle mainBundle]loadNibNamed:@"IntroducePageView" owner:self options:nil];

    self.svInstroduce.contentSize =  CGSizeMake([UIScreen mainScreen].bounds.size.width * (arIntroducePage.count+1), 0);
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    WS(ws);
    // Do any additional setup after loading the view from its nib.
    if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSArray *arIntroducePage = [[NSBundle mainBundle]loadNibNamed:@"IntroducePageView" owner:self options:nil];
    //NSLog(@"count = %lu",(unsigned long)arIntroducePage.count);
    
    self.svInstroduce.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * (arIntroducePage.count+1), 0);
    NSLog(@"svInstroduce = %@",NSStringFromCGSize(self.svInstroduce.frame.size));
    NSLog(@"frame.width = %f",_svInstroduce.contentSize.width);
    
//    IntroducePageView *viewPageLast = [arIntroducePage objectAtIndex:arIntroducePage.count-1];
//    UIButton *btn = viewPageLast.btnStart;
//    [viewPageLast.btnStart addTarget:self action:@selector(StartAction) forControlEvents:UIControlEventTouchUpInside];
    NSArray * array = [NSArray arrayWithObjects:@"tu_1",@"tu_2",@"tu_3",@"tu_4", nil];
    //NSArray * arrayT1 = [NSArray arrayWithObjects:@"年轻，充满愿望",@"行动，展现力量",@"安全，放心翱翔",@"神灯，美梦成真", nil];
    //NSArray * arrayT2 = [NSArray arrayWithObjects:@"想要的东西太多，却不知从何做起",@"零存整取，本息叠加，让愿望加速实现",@"银行级风控，一场不负所望的投资",@"年轻人的专属理财平台，让你离愿望更近一些", nil];
    
    for (NSUInteger nIndex = 0; nIndex < arIntroducePage.count; nIndex++)
    {
        UIView *viewIntroduce = [arIntroducePage objectAtIndex:nIndex];
        CGRect rect = viewIntroduce.frame;
        rect.origin.x = nIndex * [UIScreen mainScreen].bounds.size.width;
        //add by ren 2014-6-6
        if (!isiPhone5) {
            rect.origin.y -= 0;
        }
        viewIntroduce.frame = rect;
        [self.svInstroduce addSubview:viewIntroduce];
        
        UIImageView * imageStart = [[UIImageView alloc]init];
        [viewIntroduce addSubview:imageStart];
        
        [imageStart mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(viewIntroduce).offset(25);
            make.right.equalTo(viewIntroduce).offset(-25);
            make.top.equalTo(viewIntroduce).offset(85*height568);
            make.height.equalTo(@(340*height568));
        }];
        NSString * strImage = [array objectAtIndex:nIndex];
        imageStart.image = [UIImage imageNamed:strImage];


//        UILabel * lable1 = [[UILabel alloc]init];
//        lable1.text = [arrayT1 objectAtIndex:nIndex];
//        lable1.font = [UIFont fontWithName:labelCustomFont size:17];
//        lable1.textColor = [UIColor whiteColor];
//        [viewIntroduce addSubview:lable1];
//        [lable1 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(viewIntroduce);
//            make.top.equalTo(imageStart.mas_bottom).offset(60*height568);
//        }];
//        
//        UILabel * label2 = [[UILabel alloc]init];
//        label2.text = [arrayT2 objectAtIndex:nIndex];
//        label2.font = [UIFont fontWithName:labelCustomFont size:12];
//        label2.textColor = [UIColor whiteColor];
//        [viewIntroduce addSubview:label2];
//        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(viewIntroduce);
//            make.top.equalTo(lable1.mas_bottom).offset(12);
//        }];
        
//        if(isiPhone5)
//        {
//            NSLog(@"5555");
//            lable1.font = [UIFont fontWithName:labelCustomFont size:28];
//            label2.font = [UIFont fontWithName:labelCustomFont size:14];
//        }else if (isiPhone6)
//        {
//            NSLog(@"666");
//            lable1.font = [UIFont fontWithName:labelCustomFont size:32];
//            label2.font = [UIFont fontWithName:labelCustomFont size:16];
//        }else if (isiPhone6P)
//        {
//            NSLog(@"6p6p6p");
//            lable1.font = [UIFont fontWithName:labelCustomFont size:32];
//            label2.font = [UIFont fontWithName:labelCustomFont size:16];
//        }
        
        
        if(nIndex == 3)
        {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setImage:[UIImage imageNamed:@"anjian"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(closeRight) forControlEvents:UIControlEventTouchUpInside];
            [viewIntroduce addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(viewIntroduce);
                make.bottom.equalTo(viewIntroduce).offset(-25*height568);
                make.width.equalTo(@220);
                make.height.equalTo(@45);
            }];
        }
}
    
//    int h_button = 44;
//    int h_demoButton = 30;
//    int h_space = 20;
//    int h_view = h_button+h_demoButton+h_space;
    //int y_view = [[UIScreen mainScreen]bounds].size.height-h_view;
    
    //将pagecontrol添加到scrollview中
    //int h_pagectr = 30;
//    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, y_view-h_pagectr/2-h_space, 320, h_pagectr)];
    
    
    pageControl=[[UIPageControl alloc]init];
    [self.view addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(ws.view).offset(-20);
        make.left.equalTo(ws.view).offset(0);
        make.right.equalTo(ws.view).offset(0);
        make.height.equalTo(@30);
    }];
    pageControl.numberOfPages=arIntroducePage.count;
    pageControl.currentPage=0;
    [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
   
    
    [self.bt_close setHidden:YES];
    if (!isAdd) {
        //加X
        [self.bt_close setHidden:NO];
    }
    
    //int raidus = self.bt_start.frame.size.height/2;
    //[Utils setViewBorder:self.bt_start radius:raidus borderColor:UISecondaryLineColor];
}

int cuttPage;
//pagecontrol的点跟着页数改变
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1{
 
    CGPoint offset=scrollView1.contentOffset;
    CGRect bounds=scrollView1.frame;
    cuttPage = offset.x/bounds.size.width;
    [pageControl setCurrentPage:cuttPage];
    NSLog(@"cuttpage = %d",cuttPage);
    if(cuttPage == 3)
    {
        pageControl.hidden = YES;
    }else
    {
        pageControl.hidden = NO;
    }
}

// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    CGFloat xOffset   = scrollView.contentOffset.x;
//    NSLog(@"scrollViewDidScroll  xOffset:%f", xOffset);
    
    CGRect bounds=scrollView.frame;
    float floatPage = xOffset/bounds.size.width;
//    NSLog(@"scrollViewDidScroll floatPage:%f", floatPage);
    int intPage = (int)floatPage;
    float alpha = floatPage-intPage;
    
//    NSLog(@"scrollViewDidScroll alpha:%f", alpha);
    
    if (intPage==0) {
        self.backImageView1.alpha = 1.0f-alpha;
        self.backImageView2.alpha = alpha;
        self.backImageView3.alpha = 0;
    }else if (intPage==1) {
        if (floatPage<=1.0f)//向左滑动
        {
            self.backImageView1.alpha = alpha;
            self.backImageView2.alpha = 1.0f-alpha;
            self.backImageView3.alpha = 0;
        }else if (floatPage>1.0f&&floatPage<2.0)//向右滑动
        {
            self.backImageView3.alpha = alpha;
            self.backImageView2.alpha = 1.0f-alpha;
            self.backImageView1.alpha = 0;
        }
    }
    
    //如果是最后一页，再向右滑动，就退出
    int allWidth = bounds.size.width*(pageControl.numberOfPages-1);
    if(cuttPage==pageControl.numberOfPages-1
       && xOffset>allWidth){
        [self closeRight];
    }
    
}



//点击pagecontrol的点 跳到那一页的实现
- (IBAction)pageTurn:(UIPageControl *)sender {
    CGSize viewsize=self.svInstroduce.frame.size;
    CGRect rect=CGRectMake(sender.currentPage*viewsize.width, 0, viewsize.width, viewsize.height);
    [self.svInstroduce scrollRectToVisible:rect animated:YES];
}

-(IBAction)StartAction:(UIButton *)sender
{
    if (![HePreferrence hasShowIntroducePage])
    {
        //新用户
    }
    else
    {
        //老用户
    }
    
    

    //这就说明已经看过引导页面，是老用户了
    [HePreferrence setShowIntroducePage:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)closeRight{
    [UIView animateWithDuration:0.5 animations:^{
        self.svInstroduce.alpha = 0.1;
    }completion:^(BOOL finished) {
        WebViewController * controller = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
        [self.navigationController pushViewController:controller animated:YES];
        
        //这就说明已经看过引导页面，是老用户了
        [HePreferrence setShowIntroducePage:YES];
    }];
}

- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
